/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#ifndef __7FCDF8F7_3407_4528_AFC5_A15BDB01A655__
#define __7FCDF8F7_3407_4528_AFC5_A15BDB01A655__

#include "../tools/head.h"

/////////////////////////////////////////////////////////////////////////////////////////
// 

namespace xos
{
    // 
    // file_impl
    // 
    class file_impl : public xos_stl::mem_item< release_impl< file_impl >, thread_lock >,
        public i_file
    {
    public:
        typedef release_impl< file_impl > T;

    public:
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //

        static int get_xos_item_data( ITEM_DATA*& item_ptr );

    public:
        file_impl();
        ~file_impl();

    public:
        // 
        // public for easily internal use
        // 
        std::string m_file_name;
        int64_t m_cTime;
        int64_t m_aTime;
        int64_t m_mTime;
        HANDLE m_hFile;

    protected:
        int init_data();

    public:
        int init();
        int term();

    public:
        //////////////////////////////////////////////////////////////////////////////////
        // 
        // open a file
        // 
        int open( const char * lpszFileName, uint32_t ulAccessFlag, uint32_t ulShareMode, uint32_t ulCreateFlag );

        // 
        // get file time
        // 
        int get_file_time( int64_t * lpCreateTime, int64_t * lpAccessTime, int64_t * lpModifyTime );

        // 
        // get file or dir name
        // 
        const char * get_file_name( char * lpszFileName, int nLen, int * pnLenNeeded );

        // 
        // move file pointer to ...
        // 
        int set_file_pos( enumFilePos enumFrom, int64_t iPos64, int64_t * lpSeek64 );

        // 
        // get current file pointer
        // 
        int get_file_pos( int64_t * lpSize64 );

        // 
        // get file size
        // 
        int get_file_size( int64_t * lpSize64 );

        // 
        // commit file disk space.
        // 
        int commit_file( int64_t iPos64 );

        // 
        // write file data
        // 
        // -1 means failed. other , write bytes
        // 
        int write( const void * lpBuf, uint32_t ulBufSize );
        int read( void * lpBuf, uint32_t ulBufSize );

        // 
        // read file data
        // 
        // -1 means failed. other , read bytes
        //
        int write( xos_box::i_list * pList );
        int read( xos_box::i_list * pList );
        int write( xos_box::i_big_buf * pBuf );
        int read( xos_box::i_big_buf * pBuf );
        int write( xos_box::i_buf * pBuf );
        int read( xos_box::i_buf * pBuf );

        // 
        // close file handle
        // 
        int close();
    };
} // xos

#endif // __7FCDF8F7_3407_4528_AFC5_A15BDB01A655__
